<?php
App::uses('AppController', 'Controller');
/**
 * Indices Controller
 *
 * @property Index $Index
 */
class IndicesController extends AppController {

	// $helpers = array('Taxed Portfolio'); 

/**
 * index method
 *
 * @return void
 */
	public function index() {
		/*cakephp original codes
		$this->Index->recursive = 0;
		$this->set('indices', $this->paginate());
		*/
		
				        if(!empty($this->request->data)){
            $this->redirect(array('action'=>'taxedportfolio', $this->request->data['TaxedPortfolioDate']['date1']));
        }

		$con=mysqli_connect("localhost","root","","dfs");
		$sql="SELECT DISTINCT indexname From indices ORDER BY indexname";
		$temp=mysqli_query($con,$sql);
		$counter=0;
		foreach ($temp as $temp2)
		{
			foreach($temp2 as $temp3)
			{
				//get index name
				$this->Session->write("indexname[".$counter."]",$temp3);
				
				//get index id
				$sql2="SELECT indicesid FROM indices where indexname='".$temp3."' order by date desc limit 1";
				$tempID=mysqli_query($con,$sql2);
				foreach ($tempID as $tID)
				{
					foreach ($tID as $td)
					{
						$this->Session->write("indicesid[".$counter."]",$td);
					}
				} 
				$tempID->close();
				$counter ++;
			}

		}
		$this->Session->write("counter",$counter);
		$temp->close();
		$con->close();
	}

/**
 * view method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function view($id = null) {
		if (!$this->Index->exists($id)) {
			throw new NotFoundException(__('Invalid index'));
		}
		$options = array('conditions' => array('Index.' . $this->Index->primaryKey => $id));
		$this->set('index', $this->Index->find('first', $options));

		$con=mysqli_connect("localhost","root","","dfs");
		$sql="SELECT indexname from indices where indicesID = '".$id."'";
		$temp=mysqli_query($con,$sql);
		foreach ($temp as $temp2)
		{
			foreach($temp2 as $temp3)
			{
				$indexname=$temp3;
			}

		}
		$temp->close();

		$sql2="SELECT indicesid,date,OneMonthReturn,ThreeMonthReturn,SixMonthReturn,OneYearReturn,TwoYearReturn,ThreeYearReturn,FourYearReturn,FiveYearReturn,SinceInceptionReturn from indices where indexname='".$indexname."' order by date desc limit 10";
		$tempRecord=mysqli_query($con,$sql2);

		$counter=0;
		foreach ($tempRecord as $tempR)
		{
			//debug($tempR);
			$counter2=0;
			foreach($tempR as $tr)
			{
				$this->Session->write("recordIndex[".$counter."][".$counter2."]",$tr);
				$counter2++;
			}
			$counter++;
			$this->Session->write("recordIndexPer",$counter2);
		}

		$this->Session->write("recordIndexTotal",$counter);		
	}

/**
 * add method
 *
 * @return void
 */
	public function add() {
		if ($this->request->is('post')) {
			$this->Index->create();
			if ($this->Index->save($this->request->data)) {
				$this->Session->setFlash(__('The Index record has been saved'));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The Index record could not be saved. Please, try again.'));
			}
		}

		$this->loadModel('Indexname');
		$indices = $this->Indexname->find('list');
		$this->set(compact('indices'));

		$this->loadModel('Portfolioname');
		$portfolios = $this->Portfolioname->find('list');
		$this->set(compact('portfolios'));
	}

/**
 * edit method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function edit($id = null) {
		if (!$this->Index->exists($id)) {
			throw new NotFoundException(__('Invalid index'));
		}
		if ($this->request->is('post') || $this->request->is('put')) {
			if ($this->Index->save($this->request->data)) {
				$this->Session->setFlash(__('The Index record has been saved'));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The Index record could not be saved. Please, try again.'));
			}
		} else {
			$options = array('conditions' => array('Index.' . $this->Index->primaryKey => $id));
			$this->request->data = $this->Index->find('first', $options);
		}

		$this->loadModel('Indexname');
		$indices = $this->Indexname->find('list');
		$this->set(compact('indices'));

		$this->loadModel('Portfolioname');
		$portfolios = $this->Portfolioname->find('list');
		$this->set(compact('portfolios'));
	}

/**
 * delete method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function delete($id = null) {
		$this->Index->id = $id;
		if (!$this->Index->exists()) {
			throw new NotFoundException(__('Invalid index'));
		}
		$this->request->onlyAllow('post', 'delete');
		if ($this->Index->delete()) {
			$this->Session->setFlash(__('The Index record is deleted'));
			$this->redirect(array('action' => 'index'));
		}
		$this->Session->setFlash(__('The Index record was not deleted'));
		$this->redirect(array('action' => 'index'));
	}

/**
 * export csv method for taxed portfolios
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */

// public $components = array('RequestHandler', 'CsvView.CsvView');

	public function taxedportfolio($id = null) 
	{
        $this->layout = 'ajax'; 
        $this->set('event', $this->Events->findById($id)); 


	$date1array = explode("-", $date1);
    $year = $date1array[0];
    $month = $date1array[1];
    $day = $date1array[2];

    $this->set(compact('date1')); 


    } 
   // $this->viewClass = 'CsvView.Csv';
   // $results = $this->Index->find('all');
	//$excludePaths = array('Index.IndicesID', 'Index.Date', 'Index.AssignStatus', 'Index.Value', 'Index.ReturnValue', 'Index.AssignedPortfolios', 'Index.Weighting'); 
	//$customHeaders = array('Index.IndexName' => 'Benchmark', 'Index.OneMonthReturn' => '1 Month', 'Index.ThreeMonthReturn' => '3 Months', 'Index.SixMonthReturn' => '6 Months', 
	//	'Index.OneYearReturn' => '1 Year', 'Index.TwoYearReturn' => '2 Years', 'Index.ThreeYearReturn' => '3 Years', 'Index.FourYearReturn' => '4 Years', 'Index.FiveYearReturn' => '5 Years', 'Index.SinceInceptionReturn' => 'Since Inception (p.a)', 'Index.SinceInceptionReturnC' => 'Since Inception (cum)');

	//$this->CsvView->quickExport($results, $excludePaths, $customHeaders);
}
